package com.nnnu.orange.controller.user;


import com.nnnu.orange.constant.MessageConstants;
import com.nnnu.orange.pojo.dto.ReservationDto;
import com.nnnu.orange.pojo.dto.ReservationsDto;
import com.nnnu.orange.pojo.dto.ReservationsQueryDto;
import com.nnnu.orange.result.Result;
import com.nnnu.orange.service.IReservationsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 记录用户预约图书的信息 前端控制器
 * </p>
 *
 * @author zk
 * @since 2024-11-07
 */
@RestController("userReservationsController")
@RequestMapping("/user/reservations")
@Api(tags = "用户端图书预约相关接口")
@Slf4j
@RequiredArgsConstructor
public class ReservationsController {

    private final IReservationsService iReservationsService;


    /**
     * 用户预约图书
     *
     * @param dto dto
     * @return 提示
     */
    @PostMapping("/reserveBook")
    @ApiOperation("用户预约图书")
    public Result<?> reserveBook(@RequestBody @Validated ReservationDto dto) {
        log.info("用户预约图书:{}", dto);
        iReservationsService.reserveBook(dto);
        return Result.success(MessageConstants.RESERVATION_SUCCESS);
    }


    /**
     * 分页查询图书预约记录
     *
     * @param queryDto dto
     * @return 分页数据
     */
    @GetMapping("/list")
    @ApiOperation("分页查询图书预约记录")
    public Result<?> list(@Validated ReservationsQueryDto queryDto) {
        log.info("用户查询图书预约记录:{}", queryDto);
        return Result.success(iReservationsService.list(queryDto), MessageConstants.SELECT_OK);
    }


    /**
     * 修改预约记录
     *
     * @param reservationsDto dto
     * @return 提示
     */
    @PutMapping
    @ApiOperation("修改预约记录")
    public Result<?> updateReservation(@RequestBody ReservationsDto reservationsDto) {
        log.info("用户修改预约记录:{}", reservationsDto);
        iReservationsService.updateReservation(reservationsDto);
        return Result.success(MessageConstants.OPERATION_SUCCESSFUL);
    }


    /**
     * 查询预约详细信息
     *
     * @param id 预约id
     * @return 预约实体
     */
    @GetMapping("/{id}")
    @ApiOperation("查询预约详细信息")
    public Result<?> getReservationDetail(@PathVariable Integer id) {
        log.info("用户查询预约详细信息：{}", id);
        return Result.success(iReservationsService.findById(id), MessageConstants.SELECT_OK);
    }


    /**
     * 用戶取消預約
     * @param reservationId 預約ID
     * @return 提示
     */
    @PostMapping("/cancel")
    @ApiOperation("用戶取消預約")
    public Result<?> cancelReservation(@RequestBody Integer reservationId){
        log.info("用戶取消預約:{}",reservationId);
        iReservationsService.cancelReservation(reservationId);
        return Result.success(MessageConstants.OPERATION_SUCCESSFUL);
    }

}
